package com.zhkj.txg.module.product.ui

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.postDelayed
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.zhkj.lib.base.CartChangeEvent
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.RxBus
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.ui.MainActivity
import com.zhkj.txg.module.home.ui.ShopActivity
import com.zhkj.txg.module.login.ui.LoginActivity
import com.zhkj.txg.module.mine.ui.VisitLogActivity
import com.zhkj.txg.module.order.ui.OrderSubmitActivity
import com.zhkj.txg.module.product.adapter.*
import com.zhkj.txg.module.product.entity.ProductResponse
import com.zhkj.txg.module.product.entity.ServiceTagEntity
import com.zhkj.txg.module.product.entity.ShopGoodsEntity
import com.zhkj.txg.module.product.entity.SpecPriceEntity
import com.zhkj.txg.module.product.popup.ProductDetailPopupWindow
import com.zhkj.txg.module.product.popup.ProductServiceTagPopup
import com.zhkj.txg.module.product.popup.ProductSharePopupWindow
import com.zhkj.txg.module.product.popup.ProductSpecPopupWindow
import com.zhkj.txg.module.product.vm.ProductViewModel
import com.zhkj.txg.utils.ConfigManager
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.ShareUtils
import com.zhkj.txg.utils.WebViewUtils
import com.zhkj.txg.utils.WebViewUtils.addImageClickListener
import kotlinx.android.synthetic.main.activity_product_detail.*
import kotlinx.android.synthetic.main.item_product_comment.*
import kotlinx.android.synthetic.main.item_product_detail.*
import kotlinx.android.synthetic.main.item_product_group_bar.*
import kotlinx.android.synthetic.main.item_product_top.*

class ProductDetailActivity : ViewModelActivity<ProductViewModel>() {

    companion object {
        private const val PRODUCT_ID = "productId"
        private const val SHOP_ID = "SHOP_ID"
        fun normalProduct(context: Context, id: Int, singleTop: Boolean = true, shopId: Int = 0) {
            val intent = Intent(context, ProductDetailActivity::class.java).putExtra(PRODUCT_ID, id).putExtra(SHOP_ID, shopId)
            if (singleTop) intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
            context.startActivity(intent)
        }
    }

    var productId = 0//商品id
    var shopId = 0//门店id

    private val builder: StringBuilder = StringBuilder()
    private val productSpecPopupWindow: ProductSpecPopupWindow by lazy {
        ProductSpecPopupWindow(this, { specId, goodsNum -> // 立即购买按钮回调
            if (AccountManager.getToken().isEmpty()) { // 未登录，跳转登录
                LoginActivity.jump2Login(this@ProductDetailActivity)
                return@ProductSpecPopupWindow
            }
            viewModel.productLiveData.value?.response?.data?.let {
                OrderSubmitActivity.buyNow(this, it.id, specId, goodsNum)
            }
        }, { specId, goodsNum -> // 加入购物车回调
            if (AccountManager.getToken().isEmpty()) { // 未登录，跳转登录
                LoginActivity.jump2Login(this@ProductDetailActivity)
                return@ProductSpecPopupWindow
            }
//            viewModel.productLiveData.value?.response?.data?.let {
//                viewModel.addCart(AccountManager.getToken(), it.id, specId, goodsNum)
//            }
        }).apply {
            viewModel.productLiveData.value?.response?.data?.let {
//                setPlaceholder(it.image)
//                if (it.specPrice.isNotEmpty()) initData(
//                    it.spec, it.specPrice,
//                    it.shareSwitch ?: 0 == 1
//                ) else setEmptyData( // 没有属性可选时，默认值
//                    SpecPriceEntity(
//                        0, "0", "默认", it.marketPrice, it.group_price ?: "", it.price, it.storeCount,
//                        if (it.images.isNullOrEmpty()) "" else it.images[0], it.share_earn
//                    ), it.shareSwitch ?: 0 == 1
//                )
            }
        }
    }
    private val sharePicAdapter = SharePictureAdapter()
    private val servicePopupWindow: ProductServiceTagPopup by lazy {
        ProductServiceTagPopup(this)
    }
    private val tabAdapter = ProductAdapter().apply {
        setOnItemClickListener { _, _, position ->
            when (position) {
                1 -> {
                    val commentTop = commentView.top
                    val scrollY = commentTop - titleProduct.height
                    scrollProduct.smoothScrollTo(0, scrollY)
                }
                2 -> {
                    val detailTop = detailView.top
                    val scrollY = detailTop - titleProduct.height
                    scrollProduct.smoothScrollTo(0, scrollY)
                }
                else -> scrollProduct.smoothScrollTo(0, 0)
            }
        }
    }
    private val bannerAdapter = ProductBannerAdapter().apply {
        setOnItemClickListener { _, _, position ->
            if (data[position].type == BannerUIEntity.IMAGE) {
                val list: ArrayList<String> = ArrayList()
                val url = data[position].imageUrl
                var index = 0
                data.forEach {
                    if (it.type == BannerUIEntity.IMAGE) {
                        list.add(it.imageUrl ?: "")
                        if (it.imageUrl == url) index = list.lastIndex
                    }
                }
                WatcherActivity.open(this@ProductDetailActivity, list, index)
            }
        }
    }
    private val sharePopupWindow: ProductSharePopupWindow by lazy {
        ProductSharePopupWindow(this) { type ->
            when (type) {
                2 -> {
                    if (sharePopupWindow.productImage.isNotEmpty()) ProductShareActivity.open(
                        this, sharePopupWindow.goodsId,
                        sharePopupWindow.productImage.toTypedArray(),
                        sharePopupWindow.productName,
                        sharePopupWindow.productPrice
                    ) else toast(getString(R.string.empty_gallery))
                }
                1 -> ShareUtils.shareUrl2Friends(
                    this,
                    ConfigManager.getProductShareContent() ?: "",
                    sharePopupWindow.productName,
                    sharePopupWindow.productImage[0],
                    sharePopupWindow.shareUrl
                )
                else -> ShareUtils.shareUrl2Friends(
                    this,
                    ConfigManager.getProductShareContent() ?: "",
                    sharePopupWindow.productName,
                    sharePopupWindow.productImage[0],
                    sharePopupWindow.shareUrl,
                    true
                )
            }
        }
    }
    private val topPopupWindow: ProductDetailPopupWindow by lazy {
        ProductDetailPopupWindow(this) { type ->
            when (type) {
                1 -> if (AccountManager.getToken().isNotEmpty()) {
                    startActivity(Intent(this, VisitLogActivity::class.java))
                } else LoginActivity.jump2Login(this)
                2 -> Unit
                3 -> {
                    with(sharePopupWindow) {
                        viewModel.productLiveData.value?.response?.data?.let {
                            setShareEarn(if (it.share_earn.isEmpty()) "0.0" else it.share_earn)
                            goodsId = it.id.toLong()
                            productImage = it.images
                            productName = it.goodsName
                            productPrice = it.price
                            shareUrl = it.shareUrl
                            showPopupWindow()
                        }
                    }
                }
                else -> {
                    val intent = Intent(this, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
                    startActivity(intent)
                }
            }
        }
    }
    private val problemAdapter = ProblemAdapter()

    override val layoutResId: Int = R.layout.activity_product_detail
    override fun setViewModel(): ProductViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleProduct

    override fun initUI() {
        with(tabProduct) {
            layoutManager = LinearLayoutManager(this@ProductDetailActivity, LinearLayoutManager.HORIZONTAL, false)
            adapter = tabAdapter
        }
        WebViewUtils.initWebView(webView)
        webView.addImageClickListener {
            if (!it.isNullOrEmpty()) WatcherActivity.open(this@ProductDetailActivity, arrayListOf(it), 0)
        }
//        webView.setOnLongClickListener {
//            (webView.hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE).apply {
//                if (this) WatcherActivity.open(
//                    this@ProductDetailActivity, arrayListOf(webView.hitTestResult.extra ?: ""), 0
//                )
//            }
//        }
        // 设置页面的滚动监听
        scrollProduct.setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
            private val maxChangeHeight =
                ScreenUtil.getScreenWidth(this@ProductDetailActivity) * 2 / 3
            private val canBackTopHeight = ScreenUtil.getScreenHeight(this@ProductDetailActivity) * 1.5

            override fun onScrollChange(
                v: NestedScrollView?,
                scrollX: Int,
                scrollY: Int,
                oldScrollX: Int,
                oldScrollY: Int
            ) {
                if (scrollY <= 10) tabProduct.visibility = View.INVISIBLE
                else tabProduct.visibility = View.VISIBLE
                // 判断顶部透明度
                if (scrollY <= maxChangeHeight) {
                    val alpha = scrollY.toFloat() / maxChangeHeight * 255
                    titleProduct.setLeftIcon(R.drawable.ic_back)
                        .setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))
                    ivMore.isSelected = false
                } else {
                    titleProduct.setLeftIcon(R.drawable.ic_arrow_left_black)
                        .setBackgroundColor(Color.WHITE)
                    ivMore.isSelected = true
                }
                // 判断tab的位置
                when {
                    scrollY < commentView.top - titleProduct.height -> tabAdapter.selectedPosition = 0
                    scrollY < detailView.top - titleProduct.height -> tabAdapter.selectedPosition = 1
                    else -> tabAdapter.selectedPosition = 2
                }
                // 判断回到顶部的按钮是否显示
                ivProductTop.visibility = if (scrollY < canBackTopHeight) View.GONE else View.VISIBLE
            }
        })
        // 设置Banner
        with(bannerProduct) {
            adapter = bannerAdapter
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    // 判断是否滑动到特定页面
                    if (position == bannerAdapter.data.lastIndex &&
                        bannerAdapter.data.last().type == BannerUIEntity.PULL
                    ) {
                        // 滚动到商品详情
                        val detailTop = detailView.top
                        val scrollY = detailTop - titleProduct.height
                        scrollProduct.scrollTo(0, scrollY)
                        // 回滚到倒数第二个item
                        bannerProduct.postDelayed(300L) {
                            currentItem = bannerAdapter.data.lastIndex - 1
                        }
                    }
                    if (bannerAdapter.data[0].type == BannerUIEntity.VIDEO && position == 0) {
                        if (!bannerAdapter.data[0].userStop) GSYVideoManager.onResume()
                    } else if (bannerAdapter.data[0].type == BannerUIEntity.VIDEO && position > 0) {
                        if (GSYVideoManager.instance().isPlaying) {
                            bannerAdapter.data[0].userStop = false
                            GSYVideoManager.onPause()
                        }
                    }
                }
            })
        }
        // 属性查看按钮监听
        tvProductSpecName.setOnClickListener {
            if (viewModel.productLiveData.value?.response?.data?.activeType != 1 ||
                viewModel.productLiveData.value?.response?.data?.last_time ?: 0 > 0
            ) showSelectSpecDialog()
        }
        // 非会员-加入购物车按钮监听
        btnNotMemberCart.setOnClickListener {
            val map = hashMapOf("goods_id" to productId.toString(), "goods_num" to "1", "shop_id" to shopId.toString())
            viewModel.addCart(map)
//            if (TextUtils.isEmpty(AccountManager.getToken())) {
//                LoginActivity.jump2Login(this@ProductDetailActivity)
//                return@setOnClickListener
//            }
//            showSelectSpecDialog()
        }
        // 非会员 - 购买按钮点击监听
        btnNotMemberBuyNow.setOnClickListener {
            if (TextUtils.isEmpty(AccountManager.getToken())) {
                LoginActivity.jump2Login(this@ProductDetailActivity)
                return@setOnClickListener
            }
            showSelectSpecDialog()
        }
        // 会员 - 购买按钮
        btnMemberCart.setOnClickListener { btnNotMemberBuyNow.performClick() }
        // 会员 - 分享按钮
        btnMemberShare.setOnClickListener {
            with(sharePopupWindow) {
                viewModel.productLiveData.value?.response?.data?.let {
                    setShareEarn(if (it.share_earn.isEmpty()) "0.0" else it.share_earn)
                    goodsId = it.id.toLong()
                    productImage = it.images
                    productName = it.goodsName
                    productPrice = it.price
                    shareUrl = it.shareUrl
                    showPopupWindow()
                }
            }
        }
        // 回到顶部
        ivProductTop.setOnClickListener {
            scrollProduct.smoothScrollTo(0, 0)
        }
        // 发圈素材图片UI
        with(rvSharePicture) {
            hasFixedSize()
            layoutManager = GridLayoutManager(this@ProductDetailActivity, 5)
            adapter = sharePicAdapter
        }
        // 跳转发圈素材页面
        tvShareDetail.setOnClickListener {
            viewModel.productLiveData.value?.response?.data?.let {
                ShareFriendActivity.open(this, it.shareContent, it.shareImages.toTypedArray())
            }
        }
        // 跳转排名页面
        btnProductRank.setOnClickListener {
            viewModel.productLiveData.value?.response?.data?.let {
                ProductRankActivity.open(this, it.catId)
            }
        }
        // 服务说明弹窗
        tvProductService.setOnClickListener {
            viewModel.productLiveData.value?.response?.data?.service?.let {
                servicePopupWindow.setData(it)
                servicePopupWindow.showPopupWindow()
            }
        }
        // 跳转店铺首页
        ivProductHome.setOnClickListener {
//            val intent = Intent(this, MainActivity::class.java)
//            intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
//            startActivity(intent)
            val shop = viewModel.productLiveData.value?.response?.data?.shop
            if (shop == null) toast(getString(R.string.shop_is_owner))
            else ShopActivity.open(this, shop.shopName, shop.id)
        }
        // 跳转客服
        ivCustomerService.setOnClickListener {
//            VP53Manager.getInstance().EnterChatActivity(KF_53_ARG, "1", this, null)
        }
        // 商品收藏
        ivCollectionProduct.setOnClickListener {
            if (AccountManager.getToken().isEmpty()) {
                LoginActivity.jump2Login(this@ProductDetailActivity)
                return@setOnClickListener
            }
            viewModel.productLiveData.value?.response?.data?.let {
//                viewModel.collectGoods(AccountManager.getToken(), it.id.toLong())
            }
        }
        // 商品评价列表页
        tvCommentMore.setOnClickListener {
            ProductCommentListActivity.open(this, intent.getIntExtra(PRODUCT_ID, 0).toLong())
        }
        // 进入店铺
        shopView.setOnClickListener {
            viewModel.productLiveData.value?.response?.data?.shop?.let {
                if (it.id > 0) ShopActivity.open(this, it.shopName, it.id)
            }
        }
        // 顶部弹窗
        ivMore.setOnClickListener {
            topPopupWindow.showPopupWindow(it)
        }
        // 工商资质按钮
        btnQualification.setOnClickListener {
            viewModel.productLiveData.value?.response?.data?.shop?.licenceImg?.let {
                ProductLicenceActivity.open(this, it)
            }
        }
        rgProduct.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rbDetail -> {
                    webView.visibility = View.VISIBLE
                    rvProblem.visibility = View.GONE
                }
                R.id.rbProblem -> {
                    webView.visibility = View.GONE
                    rvProblem.visibility = View.VISIBLE
                }
            }
        }
        with(rvProblem) {
            layoutManager = LinearLayoutManager(this@ProductDetailActivity)
            adapter = problemAdapter
        }
    }

    override fun initData() {
//        if (intent.data != null) {
//            intent.data?.let {
//                val shareId = it.getQueryParameter("share_id")
//                val productId = it.getQueryParameter("goods_id")
//                viewModel.getProduct(productId?.toInt() ?: 0, shareId?.toInt() ?: 0)
//                return
//            }
//        }
        productId = intent.getIntExtra(PRODUCT_ID, -1)
        shopId = intent.getIntExtra(SHOP_ID, -1)
        viewModel.getProduct(productId, shopId)
    }

    override fun initViewModel() {
//        typePromotion = intent.getStringExtra(TYPE)
        // 商品数据观察者
        viewModel.productLiveData.observe(this, Observer { it ->
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS)
                it.response?.let {
                    if (it.data.activeType == 1) {
                        it.data.last_time =
                            if (it.data.endTime - it.data.startTime < 0) 0 else it.data.endTime - it.data.startTime
                    }
                    fillData(it)
                }
        })
        // 商品加入购物车观察者
        viewModel.addCartLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS)
                toast(getString(R.string.add_cart_success))
//            RxBus.post(CartChangeEvent())
//            if (productSpecPopupWindow.isShowing) productSpecPopupWindow.dismiss()
        })
        // 商品收藏观察者
        viewModel.collectProductLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS)
                viewModel.productLiveData.value?.response?.data?.let {
                    it.isCollect = it.isCollect.not()
                    val drawable =
                        getDrawable(if (it.isCollect) R.drawable.ic_product_collection_selected else R.drawable.ic_product_collection)?.apply {
                            setBounds(0, 0, intrinsicWidth, intrinsicHeight)
                        }
                    ivCollectionProduct.setCompoundDrawables(null, drawable, null, null)
                    ivCollectionProduct.text =
                        getString(if (it.isCollect) R.string.collection_success else R.string.collection_product)
                    toast(getString(if (it.isCollect) R.string.collection_success else R.string.collection_not_success))
                }
        })
        // 切换账号观察者
        viewModel.accountChangeSubscribe().observe(this, Observer { initData() })
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    private fun fillData(resp: ProductResponse) {
        with(resp.data) {
//            tv_price.text = SpanUtil.setMoneySymbol(price, false)
            tvProductPrice.text = SpanUtil.setMoneySymbol(price, true)
            // 判断是显示赚还是显示市场价
            with(tvProductEarn) {
                if (AccountManager.isMember() && share_earn.isNotEmpty() && share_earn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = share_earn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
//                    text = SpanUtil.setMoneySymbol(marketPrice)
                }
            }
            tvProductSale.text = "销量:${saleCount}"
            tvProductName.text = goodsName
            tvProductLocation.text = location
            tvProductSellTag.visibility =
                if (TextUtils.equals(isHot, "1")) View.VISIBLE else View.GONE
            tvProductNewTag.visibility =
                if (TextUtils.equals(isNew, "1")) View.VISIBLE else View.GONE
            // 判断服务Tag是否显示
            if (service.isEmpty()) btnService.visibility = View.GONE
            if (service.isNotEmpty()) tvProductService.text = getServiceText(service)
            // 判断排名是否显示
            if (rank <= 0) btnProductRank.visibility = View.GONE
            if (rank > 0) tvProductRank.text = "天兴购销售排行榜NO.${rank}"
            // 判断规格选项是否显示
            if (specPrice.isNullOrEmpty()) btnSpec.visibility = View.GONE
            if (!specPrice.isNullOrEmpty()) tvProductSpecName.text = specPrice[0].keyName
            //  判断品牌是否可见
            viewBrand.visibility = if (brand != null) View.VISIBLE else View.GONE
            if (brand != null) {
                ImageUtils.loadCircleImage(this@ProductDetailActivity, ivProductBrand, brand.image ?: "")
                tvProductBrandName.text = brand.name
            }
            // 构造顶部Banner数据
            val list = mutableListOf<BannerUIEntity>()
            if (video.isNotEmpty()) list.add(BannerUIEntity(BannerUIEntity.VIDEO, video, image))
            images.forEach { list.add(BannerUIEntity(BannerUIEntity.IMAGE, null, it)) }
            if (list.isNotEmpty()) {
                list.add(BannerUIEntity(BannerUIEntity.PULL, null, null))
                bannerProduct.offscreenPageLimit = list.size
            }
            bannerAdapter.setNewData(list)
            // 判断是否显示评论列表
            if (comment != null) {
                commentTitle.visibility = View.VISIBLE
                rvComment.visibility = View.VISIBLE
                rvComment.layoutManager = LinearLayoutManager(this@ProductDetailActivity)
                rvComment.adapter = ProductCommentAdapter().apply {
                    setNewData(mutableListOf(comment))
                }
            }
            // 判断是否显示店铺
            if (shop != null) {
                shopView.visibility = View.VISIBLE
                rvShopGoods.visibility = View.VISIBLE
                tvShopName.text = shop.shopName
                ImageUtils.loadCircleImage(this@ProductDetailActivity, ivShop, shop.shopImg)
                rvShopGoods.layoutManager = LinearLayoutManager(
                    this@ProductDetailActivity,
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
                rvShopGoods.adapter = ProductShopGoodsAdapter().apply {
                    setNewData(shop.shopGoods.apply {
                        add(ShopGoodsEntity(0).apply {
                            shopName = shop.shopName
                            shopId = shop.id.toLong()
                            itemType = 1
                        })
                    })
                }
            }
            // 判断是否显示工商资质按钮
            btnQualification.visibility =
                if (shop != null && shop.licenceImg.isNullOrEmpty().not()) View.VISIBLE else View.GONE
            // 显示详情页
            WebViewUtils.loadFullScreenHtml(webView, goodsContent)
            // 判断是否已收藏
            val drawable =
                getDrawable(if (isCollect) R.drawable.ic_product_collection_selected else R.drawable.ic_product_collection)?.apply {
                    setBounds(0, 0, intrinsicWidth, intrinsicHeight)
                }
            ivCollectionProduct.setCompoundDrawables(null, drawable, null, null)
            ivCollectionProduct.text =
                getString(if (isCollect) R.string.collection_success else R.string.collection_product)
            // 判断是否是会员
            if (AccountManager.isMember()) {
                viewNotMember.visibility = View.GONE
                viewMember.visibility = View.VISIBLE
                tvProductSave.text = "省￥${if (share_earn.isNullOrEmpty()) "0.0" else share_earn}"
                tvMemberEarn.text = "赚￥${if (share_earn.isNotEmpty()) share_earn else "0.0"}"
            } else {
                viewNotMember.visibility = View.VISIBLE
                viewMember.visibility = View.GONE
            }
            // 判断是否显示发圈素材
            if (!shareContent.isNullOrEmpty() || !shareImages.isNullOrEmpty()) {
                viewShare.visibility = View.VISIBLE
                if (shareContent.isNotEmpty()) {
                    tvShareContent.visibility = View.VISIBLE
                    tvShareContent.text = shareContent
                }
                if (shareImages.isNotEmpty()) {
                    rvSharePicture.visibility = View.VISIBLE
                    sharePicAdapter.setNewData(shareImages)
                }
            }
            // 常见问题
            problemAdapter.setNewData(problem)
            if (activeType == 1) {
                frame_group_bar.visibility = View.VISIBLE
                frame_price.visibility = View.GONE
//                countDown(last_time)
                text_price.text = SpanUtil.setMoneySymbol(price, true)
                text_count.visibility = View.GONE
                text_earn.visibility = View.GONE
                frame_progress.visibility = View.VISIBLE
                var progress = saleCount * 100 / (saleCount + storeCount)
                text_percent.text = "${progress}%"
                progress_bar.setProgress(progress)
                text_earn_promotion.apply {
                    visibility = View.VISIBLE
                    if (AccountManager.isMember() && share_earn.isNotEmpty() && share_earn != "0") {
                        paint.isAntiAlias = true
                        paint.flags = Paint.ANTI_ALIAS_FLAG
                        text = SpanUtil.setPrefixMoneyStr(price = share_earn)
                    } else {
                        paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                        paint.isAntiAlias = true
                        text = "￥${marketPrice}"
                    }
                }
                if (last_time <= 0) {
                    tvProductSoldOut.visibility = View.VISIBLE
                    btnNotMemberCart.isEnabled = false
                    btnNotMemberBuyNow.isEnabled = false
                    btnMemberShare.isEnabled = false
                    btnMemberCart.isEnabled = false
                    tvProductSaveStr.isEnabled = false
                    tvProductSave.isEnabled = false
                    tvMemberEarnStr.isEnabled = false
                    tvMemberEarn.isEnabled = false
                }
            }
        }
    }


    private fun getServiceText(service: MutableList<ServiceTagEntity>): String {
        builder.setLength(0)
        loop@ for (serviceTag in service) if (builder.length < 20) builder.append(serviceTag.title).append("\u3000")
        else break@loop
        return builder.toString()
    }

    private fun showSelectSpecDialog() {
        productSpecPopupWindow.setMode(true)
        productSpecPopupWindow.showPopupWindow()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }
}